package com.yc.web.servlets;

import com.yc.web.model.JsonModel;
import com.yc.bean.Shoes;
import com.yc.bean.User;
import com.yc.dao.RedisHelper;
import redis.clients.jedis.Jedis;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static com.yc.utils.YcConstants.*;
import static com.yc.utils.YcConstants.REDIS_SHOE_PRAISE;

@WebServlet(name = "likedServlet",value = "/praise.action")
public class likedServlet extends BaseServlet{
    //点赞功能
    protected void clickPraise(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        JsonModel jm = new JsonModel();
        //取菜品号
        Shoes shoes = null;
        try {
            shoes = super.parseObjectFromRequest(req,Shoes.class);
            int sid = shoes.getSid();

            HttpSession session = req.getSession();
            //权限  判断的这一段功能，写到   rightFilter中去
            User user = (User) session.getAttribute(USER);
            int userid = user.getUserid();

            Jedis jedis = RedisHelper.getRedisInstance();
            //菜单号_praise    Set<用户号>
            if (jedis.sismember(sid+REDIS_SHOE_PRAISE,userid+"")){
                //此用户已经点过赞      再点就是取消  商品编号_REDIS_SHOE_PRAISE
                jedis.srem(sid+REDIS_SHOE_PRAISE,userid+"");
                //用户编号_praise  Set<菜品号>     此处也要删除
                jedis.srem(userid+REDIS_PRAISE,sid+"");
                //取消后，liked值变为false
                shoes.setLiked(false);
            }else {
                //此用户没有点过赞
                jedis.sadd(sid+REDIS_SHOE_PRAISE,userid+"");
                //要记录此用户对那些菜点过赞
                jedis.sadd(userid+REDIS_PRAISE,sid+"");
                //点赞后，liked值变为true
                shoes.setLiked(true);
            }
            //取出最新点赞数
            long likes = jedis.scard(sid+REDIS_SHOE_PRAISE);
            shoes.setLikes(likes);
            jm.setCode(1);
            jm.setObj(shoes);
        }catch (Exception e){
            e.printStackTrace();
            jm.setCode(0);
            jm.setError(e.getMessage());
        }finally {
            super.writeJson(jm,resp);
        }
    }
    //点赞状态保持
    protected void praiseStatus(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        JsonModel jm = new JsonModel();
        //取菜品号
        Shoes shoes = null;
        try {
            shoes = super.parseObjectFromRequest(req,Shoes.class);
            int sid = shoes.getSid();

            HttpSession session = req.getSession();
            //权限  判断的这一段功能，写到   rightFilter中去
            User user = (User) session.getAttribute(USER);
            int userid = user.getUserid();

            Jedis jedis = RedisHelper.getRedisInstance();
            //菜单号_praise    Set<用户号>
            if (jedis.sismember(sid+REDIS_SHOE_PRAISE,userid+"")){
                //说明存在该键，liked为true
                shoes.setLiked(true);
            }else {
                //说明不存在该键，liked为false
                shoes.setLiked(false);
            }
            //取出最新点赞数
            long likes = jedis.scard(sid+REDIS_SHOE_PRAISE);
            shoes.setLikes(likes);
            jm.setCode(1);
            jm.setObj(shoes);
        }catch (Exception e){
            e.printStackTrace();
            jm.setCode(0);
            jm.setError(e.getMessage());
        }finally {
            super.writeJson(jm,resp);
        }
    }

}
